<?php

namespace app\admin\controller;

use app\admin\model\special\Special;
use app\admin\model\store\StoreProduct;
use app\admin\model\system\SystemConfig;
use app\admin\model\system\SystemMenus;
use app\admin\model\system\SystemRole;
use app\admin\model\order\StoreOrder as StoreOrderModel;//订单
use app\admin\model\user\UserExtract as UserExtractModel;//分销
use app\admin\model\user\User as UserModel;//用户
use app\admin\model\store\StoreProductReply as StoreProductReplyModel;//评论
use app\admin\model\store\StoreProduct as ProductModel;//产品
use FormBuilder\Json;
use think\DB;
use app\admin\model\system\Statistics;

use service\JsonService;
/**
 * 首页控制器
 * Class Index
 * @package app\admin\controller
 *
 */
class Index extends AuthController
{
    public function index()
    {
        //获取当前登录后台的管理员信息
        $adminInfo = $this->adminInfo;
        $roles  = explode(',',$adminInfo['roles']);

        $this->assign([
            'menuList'=>SystemMenus::menuList(),
            'role_name'=>SystemRole::where('id',$roles[0])->field('role_name')->find()
        ]);

        return $this->fetch();
    }

    //后台首页内容
    public function main()
    {
        return $this->fetch();
    }

    /**
     * 数据看盘
     */ 
    public function pannel()
    {
        return $this->fetch();
    }

    // 视频号小店服务端API测试
    public function test()
    {
       //$res = (new \service\WechatChannel)->getProductList();
       //$res = (new \service\WechatChannel)->getCategoryList();
       $res = (new \service\WechatChannel)->addProduct(3);
       return JsonService::successful($res);
    }

    /**
     * 本地上传-
     * @Author   FW
     * @DateTime 2024-02-28
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function image_upload()
    {
        try{
            $file = $this->request->file('file');
            $path = $this->request->post('path') ?? 'images';

            /** 上传图片尺寸约束 */
            $size_control = $this->request->post('size_control' , '');
            $size_strict  = (boolean)$this->request->post('size_strict' , 'false');
            if ( strpos($size_control , '*') ) {
                list($size_w , $size_h) = explode('*' , $size_control);

                if ( $size_w && $size_h ) {
                    $img_size_info = @getimagesize($file->getInfo('tmp_name'));
                    if ( isset($img_size_info[0]) && isset($img_size_info[1]) ) {
                        if ( $size_strict ) {
                            if ( $size_w != $img_size_info[0] || $size_h != $img_size_info[1] ) {
                                return json_encode(["code"=> 400,'state'=>'上传文件尺寸不符合!请上传'.$size_control.'尺寸图片']);
                            }
                        } else {
                            $ratio_ori = $size_w / $size_h;
                            $ratio_trs = $img_size_info[0] / $img_size_info[1];
                            if ( $ratio_ori != $ratio_trs ) {
                                return json_encode(["code"=> 400,'state'=>'上传文件尺寸不符合!请上传'.$size_control.'等比例尺寸图片']);
                            }

                        }
                    }
                }
            }
            /** 上传图片尺寸约束 */

            $extType=['jpg','jpeg','png','gif'];
            $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
            $size=5097152;
            if($file->getInfo('size') > $size) return json_encode(['state'=>'上传文件太大']);
            if(!in_array($ext,$extType)) return json_encode(['state'=>'上传文件类型不符合!请上传JPG,JPEG,PNG,GIF图片']);


            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $path);
            if($info){
                $key = '/public' . DS . 'uploads' . DS . $path . DS . $info->getSaveName();

                 return json_encode([
                    "code"=> 0,
                    "pic" => request()->domain().$key,
                ]);
            }else{
                return json_encode([
                    "code"=> 400,
                    "state" => $file->getError()
                ]);
            }
        }catch (\Exception $e){
            return json_encode(["code"=> 400,'state'=>$e->getMessage()]);
        }
    }

    /**
     * 金山云上传
     * @Author   FW
     * @DateTime 2021-06-11
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function jsy_upload()
    {
        try{
            $file = $this->request->file('file');
            $path = $this->request->post('path') ?? 'images';

            /** 上传图片尺寸约束 */
            $size_control = $this->request->post('size_control' , '');
            $size_strict  = (boolean)$this->request->post('size_strict' , 'false');
            if ( strpos($size_control , '*') ) {
                list($size_w , $size_h) = explode('*' , $size_control);

                if ( $size_w && $size_h ) {
                    $img_size_info = @getimagesize($file->getInfo('tmp_name'));
                    if ( isset($img_size_info[0]) && isset($img_size_info[1]) ) {
                        if ( $size_strict ) {
                            if ( $size_w != $img_size_info[0] || $size_h != $img_size_info[1] ) {
                                return json_encode(["code"=> 400,'state'=>'上传文件尺寸不符合!请上传'.$size_control.'尺寸图片']);
                            }
                        } else {
                            $ratio_ori = $size_w / $size_h;
                            $ratio_trs = $img_size_info[0] / $img_size_info[1];
                            if ( $ratio_ori != $ratio_trs ) {
                                return json_encode(["code"=> 400,'state'=>'上传文件尺寸不符合!请上传'.$size_control.'等比例尺寸图片']);
                            }

                        }
                    }
                }
            }
            /** 上传图片尺寸约束 */

            $extType=['jpg','jpeg','png','gif'];
            $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
            $size=5097152;
            if($file->getInfo('size') > $size) return json_encode(['state'=>'上传文件太大']);
            if(!in_array($ext,$extType)) return json_encode(['state'=>'上传文件类型不符合!请上传JPG,JPEG,PNG,GIF图片']);
            $key = $path . '/' .time() . rand(0, 9999) . '.' . $ext;
            $res = \service\JSYService::upLoaderFile($file->getRealPath(),$key);
            if($res['ETag']){
                return json_encode([
                    "code"=> 0,
                    "key" => $key,
                    "pic" => 'https://ks3-cn-beijing.ksyun.com/teaseyoulearn/'.$key,
                ]);
            }else{
                return json_encode([
                    "code"=> 400,
                    "state" => "ERROR"
                ]);
            }
        }catch (\Exception $e){
            return json_encode(["code"=> 400,'state'=>$e->getMessage()]);
        }
    }
} //Class End


